package logic

import (
	"2211a_mayi/service/internal/svc"
	"2211a_mayi/service/mayi"
	"2211a_mayi/service/models"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodsLogic {
	return &CreateGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateGoodsLogic) CreateGoods(in *mayi.CreateGoodsRequest) (*mayi.CreateGoodsResponse, error) {
	var g models.Goods
	g.GoodsTitle = in.GoodsTitle
	g.GoodsCover = in.GoodsCover
	g.GoodsPrice = float64(in.GoodsPrice)
	g.GoodsStock = int(in.GoodsNum)
	err := g.Create()
	if err != nil {
		logx.Error(err)
		return nil, err
	}

	return &mayi.CreateGoodsResponse{
		GoodsId: int64(g.ID),
	}, nil
}
